import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// import vuex modules
import user from './user'
import info from './info'
import header from './header'
import footer from './footer'
// toast消失时间
const TOAST_DISAPEAR_TIME = 1200
const store = new Vuex.Store({
  state: {
    loading: false,
    phoneNumber: '',
    accessToken: '',
    toast: {
      show: false,
      message: ''
    }
  },
  mutations: {
    UPDATE_LOADING (state, n) {
      if (n === false) {
        state.loading = n
        return
      }
      state.loading = !state.loading
    },
    UPDATE_PHONE_NUMBER (state, n) {
      state.phoneNumber = n
    },
    UPDATE_ACCESS_TOKEN (state, s) {
      state.accessToken = s
    },
    UPDATE_TOAST (state, payload) {
      state.toast = {
        show: true,
        message: payload.message,
        fn: payload.fn
      }
      setTimeout(() => {
        state.toast = {show: false}
        payload.fn && payload.fn()
      }, payload.timeout || TOAST_DISAPEAR_TIME)
    }
  },
  modules: {
    user,
    info,
    header,
    footer
  }
})

export default store
